Interactive Plotting with Plotly#
Why Use Plotly?#
Most plots you’ve made so far (with matplotlib or seaborn) are static — once you export them, you can’t zoom, hover, or explore them.
Plotly lets you create interactive plots where you can:
Hover over data points for values
Zoom, pan, and export plots
Highlight experimental trends visually
Create web-based reports or dashboards
Built-In Tools#
All Plotly plots include:
Zoom and pan
Hover for exact values
Export to PNG or SVG
Reset view button
You can also save a plot to HTML:
fig.write_html("my_interactive_plot.html")
Use Cases in Biochemistry:#
Explore time-course data (e.g., enzyme kinetics)
Visualize sample comparisons interactively
Build dashboards for high-throughput data
Plotly works well in Jupyter Notebooks and can also export to HTML or PNG.
Let’s start with a simple enzyme activity example!
# You may need to install plotly first (only once):
!pip install plotly
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
#notebook renderer
import plotly.io as pio
pio.renderers.default = 'notebook'
Requirement already satisfied: plotly in c:\users\benhuang\anaconda3\envs\data-book\lib\site-packages (6.2.0)
Requirement already satisfied: narwhals>=1.15.1 in c:\users\benhuang\anaconda3\envs\data-book\lib\site-packages (from plotly) (1.44.0)
Requirement already satisfied: packaging in c:\users\benhuang\appdata\roaming\python\python313\site-packages (from plotly) (25.0)
Interactive Line Plot: Enzyme Activity#
You measured enzyme activity every 5 minutes. Let’s plot the data interactively.
# Sample data
time_min = [0, 5, 10, 15, 20, 25, 30]
activity = [0.0, 1.9, 3.8, 5.3, 6.8, 7.5, 8.0]
# Create a line plot
fig = go.Figure()
fig.add_trace(go.Scatter(
x=time_min,
y=activity,
mode='lines+markers',
name='Enzyme Activity',
line=dict(color='blue'),
marker=dict(size=8)
))
# Add layout
fig.update_layout(
title='Enzyme Activity Over Time',
xaxis_title='Time (minutes)',
yaxis_title='Activity (µmol/min)',
template='plotly_white'
)
fig.show()
Bar Chart: Comparing Treatment Groups#
You ran an ELISA and want to compare protein levels in 3 groups interactively.
# Create a sample dataset
df = pd.DataFrame({
'Group': ['Control', 'Treated A', 'Treated B'],
'Protein (µg/mL)': [1.2, 2.6, 2.1]
})
# Use plotly express for simple bar plot
fig = px.bar(df, x='Group', y='Protein (µg/mL)',
color='Group',
title='Protein Concentration by Group',
text='Protein (µg/mL)')
fig.update_traces(textposition='outside')
fig.update_layout(template='plotly_white')
fig.show()
Summary#
In this chapter, you learned how to:
Create interactive plots with plotly
Visualize time-course data (enzyme activity)
Compare treatment groups using interactive bar charts
Export your plots to HTML or PNG
Plotly is perfect for building interactive dashboards or exploring large experimental datasets.
You can use it alongside matplotlib or seaborn — depending on whether you need publication-ready figures or interactive exploration.